/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: PopupRadioButton.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.baselib.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioButton;

import io.bhex.baselib.R;
import io.bhex.baselib.utils.PixelUtils;
import skin.support.widget.SkinCompatRadioButton;

/**
 * 带提示Pupup的RadioButton
 * ================================================
 */
public class PopupRadioButton extends SkinCompatRadioButton {
//public class PopupRadioButton extends RadioButton {

    private float aspectRatio;

    private Drawable popupRes;

    private int popupVisibility = View.GONE;

    public PopupRadioButton(Context context) {
        super(context);
        init(context, null);
    }

    public PopupRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public PopupRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);

    }

    /**
     * 初始化View
     *
     * @param context
     * @param attrs
     */
    private void init(Context context, AttributeSet attrs) {
//        setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        // 设置不显示Button
        setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));

        // 得到属性
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs,
                    R.styleable.PopupRadioButton);
            boolean isPopupShow = ta.getBoolean(
                    R.styleable.PopupRadioButton_showPopup, false);

            popupRes = ta.getDrawable(R.styleable.PopupRadioButton_popupSrc);

            aspectRatio = ta.getFraction(R.styleable.PopupRadioButton_aspectRatio, 1, 100, 0.5f);

            if (isPopupShow) {
                popupVisibility = View.VISIBLE;
            } else {
                popupVisibility = View.GONE;
            }

            ta.recycle();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        adjustDrawBounds();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (popupVisibility == View.VISIBLE) {
            if (popupRes != null) {
                popupRes.draw(canvas);
            }

        }
    }


    private void adjustDrawBounds() {

        float btnHeight = getMeasuredHeight();
        Drawable[] drawables = getCompoundDrawables();
        Drawable topDrawable = drawables[1];
        if (topDrawable != null) {
            float scale = 1;
            if (topDrawable instanceof BitmapDrawable || topDrawable instanceof StateListDrawable) {
                scale = topDrawable.getIntrinsicHeight() * 1.0f / topDrawable.getIntrinsicWidth();
            }

            int width, max;
            if (scale > 1) {
                width = (int) (aspectRatio * btnHeight / scale);
                max = (int) (width * scale);
            } else {
                width = (int) (aspectRatio * btnHeight);
                max = width;
            }

            //因为整体设置过居中了，所以就不需要调整水平方向的位置了
            int left = 0;// (max - width) / 2;
            int top = (max - (int) (width * scale)) / 2;
            int right = width + left;
            int bottom = (int) (width * scale) + top;
            topDrawable.setBounds(left, top, right, bottom);

            setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
        }

        //调整提示信息位置
        if (popupRes != null) {
            float btnWidth = getMeasuredWidth();
            int width, height;

            if (popupRes instanceof BitmapDrawable || popupRes instanceof StateListDrawable) {
                width = popupRes.getIntrinsicWidth();
                height = popupRes.getIntrinsicHeight();
            } else {
                width = PixelUtils.dp2px(30);
                height = width;
            }

            Rect drawableBounds = topDrawable.getBounds();
            int left = (int) (btnWidth / 2 + drawableBounds.width() / 2);
            int bottom = getPaddingTop() + drawableBounds.top + height / 2;
            popupRes.setBounds(left, bottom - height, left + width, bottom);
        }

    }

    /**
     * 显示Popup
     */
    public void showPopup() {
        this.popupVisibility = View.VISIBLE;
        invalidate();
    }

    /**
     * 隐藏Popup
     */
    public void hidePopup() {
        this.popupVisibility = View.GONE;
        invalidate();
    }
}
